package com.diankangwo.factory.controller;

import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.common.validator.ValidatorUtils;
import com.diankangwo.common.validator.group.UpdateGroup;
import com.diankangwo.factory.entity.TdPort;
import com.diankangwo.factory.entity.TdPortSetting;
import com.diankangwo.factory.service.TdPortService;
import com.diankangwo.factory.service.TdPortSettingService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 端口设置Controller
 */
@RestController
@RequestMapping("/fac/portsetting")
public class TdPortSettingController {
    @Autowired
    private TdPortSettingService tdPortSettingService;
    @Autowired
    private TdPortService tdPortService;


    /**
     * 修改端口设置
     *
     * @param tdPortSetting
     * @return
     */
    @ApiOperation(value = "修改端口设置", notes = "")
    @ApiImplicitParam(name = "tdPortSetting", value = "端口设置对象", required = true, dataType = "TdPortSetting")
    @SysLog("修改端口设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @RequiresPermissions("fac:portsetting:update")
    public R update(@RequestBody TdPortSetting tdPortSetting) {
        ValidatorUtils.validateEntity(tdPortSetting, UpdateGroup.class);
        tdPortSettingService.updateByPrimaryKey(tdPortSetting);
        Map<String, Object> params = new HashMap<>();
        params.put("page", 1);
        params.put("limit", Integer.MAX_VALUE);
        Query query = new Query(params);
        List<TdPort> tdPortList = tdPortService.queryList(query);
        if (tdPortList.get(0).getPrice() != tdPortSetting.getPortprice()) {
            for (int i = 0; i < tdPortList.size(); i++) {
                TdPort tdPort = new TdPort();
                tdPort = tdPortService.queryById(tdPortList.get(i).getId());
                tdPort.setPrice((float) tdPortSetting.getPorttransferprice());
                tdPort.setPresentValue((float) tdPortSetting.getPorttransferprice());
                tdPortService.update(tdPort);
            }
        }

        return R.ok();
    }

    /**
     * 查询端口
     *
     * @return
     */
    @SysLog("查询端口设置")
    @ApiOperation(value = "查询端口", notes = "")
    @ApiImplicitParam(name = "tdPortSetting", value = "端口设置对象", required = true, dataType = "TdPortSetting")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @RequiresPermissions("fac:portsetting:find")
    public R save() {
        TdPortSetting tdPortSetting = tdPortSettingService.selectByPrimaryKey(1);
        return R.ok().put("tdPortSetting", tdPortSetting);
    }
}
